package com.xiangxiao.rpan.data.service;


import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import com.xiangxiao.rpan.data.channel.GreetingsStreams;
import com.xiangxiao.rpan.data.model.Greetings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2024/5/30 17:09
 */
@Service
@EnableBinding(value = {GreetingsStreams.class})
@Slf4j
public class GreetingsService {
  private final GreetingsStreams greetingsStreams;

  public GreetingsService(GreetingsStreams greetingsStreams) {
    this.greetingsStreams = greetingsStreams;
  }

  public void sendGreeting(final Greetings greetings) {
    log.info("Sending greetings {}", greetings);

    MessageChannel messageChannel = greetingsStreams.outboundGreetings();
    messageChannel.send(MessageBuilder
        .withPayload(greetings)
        .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
        .build());
  }
}
